#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setMaximumSize(400,400);//##设置窗口大小
    this->setMinimumSize(400,400);
    this->setWindowTitle("计算器");

    QFont f("仿宋",15);//##设置字体
    ui->MainlineEdit->setFont(f);

    //##按钮上放图片
    QIcon con("D:\\QT\\QtProject\\2_calculator\\tt.png");
    ui->DelButton->setIcon(con);

    //##D改变按键风格
    ui->EqualButton->setStyleSheet("background:blue");

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_Button_1_clicked() //按键1
{
    expression +="1";
    ui->MainlineEdit->setText(expression);
}

void Widget::on_C_Button_clicked()//清空
{
    expression.clear();
    ui->MainlineEdit->clear();
}

void Widget::on_DelButton_clicked()//删除一个
{
    expression.chop(1);
    ui->MainlineEdit->setText(expression);
}

void Widget::on_Button_2_clicked()
{
    expression +="2";
    ui->MainlineEdit->setText(expression);
}

void Widget::on_Button_3_clicked()
{
    expression +="3";
    ui->MainlineEdit->setText(expression);
}

void Widget::on_Button_4_clicked()
{
    expression +="4";
    ui->MainlineEdit->setText(expression);
}

void Widget::on_Button_5_clicked()
{
    expression +="5";
    ui->MainlineEdit->setText(expression);
}

void Widget::on_Button_6_clicked()
{
    expression +="6";
    ui->MainlineEdit->setText(expression);
}

void Widget::on_Button_7_clicked()
{
    expression +="7";
    ui->MainlineEdit->setText(expression);
}

void Widget::on_Button_8_clicked()
{
    expression +="8";
    ui->MainlineEdit->setText(expression);
}

void Widget::on_Button_9_clicked()
{
    expression +="9";
    ui->MainlineEdit->setText(expression);
}

void Widget::on_Button_L_clicked()
{
    expression +="(";
    ui->MainlineEdit->setText(expression);
}

void Widget::on_Button_0_clicked()
{
    expression +="0";
    ui->MainlineEdit->setText(expression);
}

void Widget::on_Button_R_clicked()
{
    expression +=")";
    ui->MainlineEdit->setText(expression);
}

void Widget::on_AddButton_clicked()
{
    expression +="+";
    ui->MainlineEdit->setText(expression);
}

void Widget::on_SubButton_clicked()
{
    expression +="-";
    ui->MainlineEdit->setText(expression);
}

void Widget::on_MluButton_clicked()
{
    expression +="*";
    ui->MainlineEdit->setText(expression);
}

void Widget::on_DivButton_clicked()
{
    expression +="/";
    ui->MainlineEdit->setText(expression);
}



void Widget::on_EqualButton_clicked()//等于 计算器功能实现
{
    QStack<int> s_num,s_opt; //存储计算数字，存储运算符
    char opt[128] = {0};
    int i=0,tmp = 0, num1,num2;
    //把QString 转换成 char*
    QByteArray ba;
    ba.append(expression);//把QString转换成 QByteArray
    strcpy(opt,ba.data());//把QByteArray转换成 char*

    while(opt[i]!='\0' || s_opt.empty()!= true)
    {
        if(opt[i]>='0' && opt[i]<='9') //如果是数字
        {
            tmp = tmp *10 + opt[i]-'0';
            i++;
            if(opt[i]<'0' || opt[i]>'9')
            {
                 s_num.push(tmp);//将当前数字放入栈中
                 tmp = 0;
            }
        }
        else//操作符
        {
            //如果（s_opt为空 或 当前优先级比上一个操作符的大 或 操作符上一个是‘（’当前是‘）’）
            if(s_opt.empty()==true || Priority(opt[i])>Priority(s_opt.top()) || (s_opt.top() =='(' && opt[i]!=')'))
            {
                s_opt.push(opt[i]);
                i++;
                continue;
            }
            if(s_opt.top() =='(' && opt[i]==')')
            {
                s_opt.pop();
                i++;
                continue;
            }
            //如果（当前优先级小于 操作符优先级 或 当前遇到‘）’且上一个操作符不是‘（’ 或 字符串结尾且操作符栈仍不为空）
            if(Priority(opt[i]) <= Priority(s_opt.top()) || (opt[i] ==')' && s_opt.top() !='(') ||
                    (opt[i] == '\0' && s_opt.empty() !=true))
            {
                char ch = s_opt.top();
                s_opt.pop();
                switch(ch)
                {
                    case '+':
                    num1=s_num.top();
                    s_num.pop();
                    num2 =s_num.top();
                    s_num.pop();
                    s_num.push(num1 + num2);
                    break;
                    case '-':
                    num1=s_num.top();
                    s_num.pop();
                    num2 =s_num.top();
                    s_num.pop();
                    s_num.push(num2 - num1);
                    break;
                    case '*':
                    num1=s_num.top();
                    s_num.pop();
                    num2 =s_num.top();
                    s_num.pop();
                    s_num.push(num1 * num2);
                    break;
                    case '/':
                    num1=s_num.top();
                    s_num.pop();
                    num2 =s_num.top();
                    s_num.pop();
                    s_num.push(num2 / num1);
                    break;
                }
            }
        }
    }
    ui->MainlineEdit->setText(QString::number(s_num.top()));//把int型转为 QString
    expression.clear();
}
int Widget::Priority(char ch)
{
    switch(ch)
    {
        case '(':
            return 3;
        case '*':
        case '/':
            return 2;
        case '+':
        case '-':
            return 1;
        default:
            return 0;
    }
}

